<?php 
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))
  	header("location:../../index.php");
}
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	$message = '';

	if(isset($_SESSION['iduser'])){
		//$idAgent = idAgent($database_microassur, $microassur, $_SESSION['iduser']); //var_dump($idAgent);
		//var_dump($_SESSION['iduser']);
	}
?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if(isset($_GET['modif']))
	$modif = $_GET['modif'];
else
	$modif = 0;	

$date_du_jour = date('Y-m-d');
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
 $insertSQL = sprintf("INSERT INTO evenement (Identifiant_evenement, Type_evenement, Sous_type_evenement, Motif_evenement, Reference_evenement, Date_evenement, Identifiant_agent, Identifiant_sinistre, Identifiant_contrat, Identifiant_personne, Identifiant_echeance, Identifiant_prestation) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_evenement'], "int"),
                       GetSQLValueString($_POST['Type_evenement'], "text"),
                       GetSQLValueString($_POST['Sous_type_evenement'], "text"),
                       GetSQLValueString($_POST['Motif_evenement'], "text"),
                       GetSQLValueString($_POST['Reference_evenement'], "text"),
                       GetSQLValueString($_POST['Date_evenement'], "date"),
                       GetSQLValueString($_POST['Identifiant_agent'], "int"),
                       GetSQLValueString($_POST['Identifiant_sinistre'], "int"),
                       GetSQLValueString($_POST['Identifiant_contrat'], "int"),
                       GetSQLValueString($_POST['Identifiant_personne'], "int"),
                       GetSQLValueString($_POST['Identifiant_echeance'], "int"),
                       GetSQLValueString($_POST['Identifiant_prestation'], "int"));
	
  mysql_select_db($database_microassur, $microassur);
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  mysql_query("START TRANSACTION");
  if($Result1){
		$updateSQL2 = sprintf("UPDATE role SET Etat_role='A' WHERE Etat_role='I' AND Identifiant_contrat=%s", GetSQLValueString($_POST['Identifiant_contrat'], "int"));
		$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());
		if($Result2){
			$message = colorer("Modification rôle, réactivation contrat prise en compte.", "true");
		}else{
			$message = colorer("Problème modification rôle, réactivation contrat, contactez la DSI.", "false");
		}
		$updateSQL3 = sprintf("UPDATE garantiesouscrite SET Etat_garantie_souscrite='A' WHERE Etat_garantie_souscrite='I' AND Identifiant_contrat=%s", GetSQLValueString($_POST['Identifiant_contrat'], "int"));
		$Result3 = mysql_query($updateSQL3, $microassur) or die(mysql_error());
		if($Result3){
			$message = colorer("Modification garantie souscrite, réactivation contrat, prise en compte.", "true");
		}else{
			$message = colorer("Problème modification garantie souscrite, réactivation contrat, contactez la DSI.", "false");
		}
		$dateEffet = dateEffetContrat($database_microassur, $microassur, GetSQLValueString($_POST['Identifiant_contrat'], "int"));
		$updateSQL4 = sprintf("UPDATE contrat SET Etat_contrat='A', Date_echeance_contrat =%s WHERE Identifiant_contrat=%s", 
								GetSQLValueString(date_plus_1an_moins_1jour($dateEffet), "date"),
								GetSQLValueString($_POST['Identifiant_contrat'], "int"));
				
		$Result4 = mysql_query($updateSQL4, $microassur) or die(mysql_error());
		if($Result4){
			$message = colorer("La réactivation du contrat est prise en compte.", "true");
		}else{
			$message = colorer("Problème modification contrat, réactivation contrat, contactez la DSI.", "false");
		}
		mysql_query("commit");
  }else{
	mysql_query("rollback");
  	$message = colorer("Problème création évènement, réactivation contrat, contactez la DSI.", "false");
  }
}


$maxRows_rs1 = 10;
$pageNum_rs1 = 0;
if (isset($_GET['pageNum_rs1'])) {
  $pageNum_rs1 = $_GET['pageNum_rs1'];
}
$startRow_rs1 = $pageNum_rs1 * $maxRows_rs1;

$colname_rs1 = "-1";

if ( ((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1")) || ((isset($_GET["MM_rechercher"])) && ($_GET["MM_rechercher"] == "form1")) ) {
	if (isset($_POST['Numero_contrat']) && !empty($_POST['Numero_contrat'])) {
	  $numero_contrat = $_POST['Numero_contrat'];
	  $colname_rs1 = IdContrat($database_microassur, $microassur, $numero_contrat);
	}
	
	if(isset($_GET['Numero_contrat']) && !empty($_GET['Numero_contrat'])){
	  $numero_contrat = $_GET['Numero_contrat'];
	  $colname_rs1 = IdContrat($database_microassur, $microassur, $numero_contrat);
	}

	mysql_select_db($database_microassur, $microassur);
	//$query_rs1 = sprintf("SELECT * FROM `role`  WHERE Etat_role = 'I' AND Identifiant_contrat = %s", GetSQLValueString($colname_rs1, "int"));
	$query_rs1 = sprintf("SELECT * FROM `role`  WHERE Identifiant_contrat = %s", GetSQLValueString($colname_rs1, "int"));
	//$query_limit_rs1 = sprintf("%s LIMIT %d, %d", $query_rs1, $startRow_rs1, $maxRows_rs1);
	$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
	$row_rs1 = mysql_fetch_assoc($rs1);
	
	if (isset($_GET['totalRows_rs1'])) {
	  $totalRows_rs1 = $_GET['totalRows_rs1'];
	} else {
	  $all_rs1 = mysql_query($query_rs1);
	  $totalRows_rs1 = mysql_num_rows($all_rs1);
	  //$totalPages_rs1 = ceil($totalRows_rs1/$maxRows_rs1)-1;

	//**************************
	mysql_select_db($database_microassur, $microassur);
	$query_rs2 = sprintf("SELECT * FROM `contrat`  WHERE Identifiant_contrat = %s", GetSQLValueString($colname_rs1, "int"));
	$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());
	$row_rs2 = mysql_fetch_assoc($rs2);

	$all_rs2 = mysql_query($query_rs2);
	$totalRows_rs2 = mysql_num_rows($all_rs2);
	//**************************
	
	$etatContrat = EtatContrat($database_microassur, $microassur, @$numero_contrat);

	$motifEvenement = getMotifEvenement($database_microassur, $microassur, GetSQLValueString($colname_rs1, "int"));
	
	if((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1")){	
		if(isset($_POST['Numero_contrat']) && empty($_POST['Numero_contrat'])){
			$message = colorer("Vous devez saisir le numéro de contrat", "false");
		}elseif(isset($_POST['Numero_contrat']) && !veriflibelle1('Numero_contrat')){
			$message = colorer("Le numéro de contrat saisi comporte des caractères spéciaux interdits.","false");
		}elseif( $totalRows_rs2==0){
			$message = colorer("Contrat inexistant.", "false");
		}elseif($totalRows_rs1==0){
			$message = colorer("", "false"); //colorer("Aucun role n'est rattaché à ce contrat.", "false"); // Contrat inexistant ou n'est rattaché à aucun rôle
		}elseif($etatContrat == 'A'){
			$message = colorer("Vous ne pouvez pas réactiver un contrat qui n’est pas résilié.", "false");
		}elseif(strcmp($motifEvenement, 'NP')==0){
			$message = colorer("Vous ne pouvez pas réactiver un contrat résilié pour non paiement.", "false");
		}elseif(strcmp($motifEvenement, 'S')==0){
			$message = colorer("Vous ne pouvez pas réactiver un contrat dont le souscripteur est sinistré.", "false");
		}elseif(strcmp($motifEvenement, 'RE')==0){
			$message = colorer("Vous ne pouvez pas réactiver un contrat suite à une renonciation.", "false");
		}elseif(strcmp($motifEvenement, 'CC')==0){
			$message = colorer("Vous ne pouvez pas réactiver un contrat ayant fait l’objet d’un changement de couverture .", "false");
		}else{
			;
		}
	}
}
} // Fin if ((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1"))

if(isset($_GET['annul']) && $_GET['annul']==0){
	$message = colorer("Vous devez cliquer sur le bouton Valider car vous avez modifié un des intervenants.", "false");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Réactivation contrat</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
			<script language="Javascript">
				<!--
					function modifier(choix){
						var modifAct = document.getElementById("modifAct");
						var suppAct = document.getElementById("suppAct");
						<?php if(strcmp($motifEvenement, 'AL')==0 || strcmp($motifEvenement, 'PA')==0){ ?>
							modifAct.href = 'detailIntervenant.php?Identifiant_personne=' + choix + '&Numero_contrat=' + '<?php echo $numero_contrat; ?>' + '&modif=' + '<?php echo $modif; ?>&visu=0';
						<?php } ?>
						suppAct.href = 'detailIntervenant.php?Identifiant_personne=' + choix + '&Numero_contrat=' + '<?php echo $numero_contrat; ?>' + '&modif=' + '<?php echo $modif; ?>&visu=1';
					}
				//-->
            </script>
	</head> 

<body>
<div id="sf_menu1">
	<h3>Production</h3>
<?php
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Réactivation contrat";
	$nom_module_saisie="Liste des intervenants";
?>
</div>
<div class="cleared"></div>

<div class="corps1">

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div id="rech">
		<label>N°&nbsp;CONTRAT</label>
		<input id="Numero_contrat"  class="resize_1" type="text" name="Numero_contrat" value="<?php if(isset($_GET['Numero_contrat'])){ echo $_GET['Numero_contrat']; } else{ echo remind_text('Numero_contrat');} ?>" size="32" maxlength="20" />
        <div class="bt">
            <span class="btleft"></span>
            <input type="submit" name="Rechercher" value="Rechercher" class="resizeall btmiddle" />      	  
            <span class="btright"></span>
        </div>  
	</div>
	<input type="hidden" name="MM_rechercher" value="form1" />
</form>

<div id="br"></div><div id="br"></div>

	<div class="imgleft2"></div>
	<div class="imgmiddle2">
		<div class="btn">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nommodule; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="btn1">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nom_module_saisie; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="cleared"></div>
<table class="tab_list">
  <tr id="niv1">
    <td class="col1">&nbsp;</td>
    <td class="col2">ROLE</td>
    <td class="col3">TYPE ASSURE</td>
    <td class="col4">NOM</td>
    <td class="col5" class="col1">PRENOM</td>
    <td class="col6">DATE NAISSANCE</td>
	<!--td class="col7">&nbsp;</td-->
  </tr>
</table>

<div class="content_table">
<table class="tab_list" style="float:left;">
  <?php 
$afficherMotif = false;
if(isset($row_rs1['Identifiant_personne']) && !empty($row_rs1['Identifiant_personne']) && $etatContrat == 'R' && strcmp($motifEvenement, 'NP')!=0  && strcmp($motifEvenement, 'S')!=0  && strcmp($motifEvenement, 'RE')!=0  && strcmp($motifEvenement, 'CC')!=0){ 
	$i=1; 
  do { 
  $afficherMotif = true;
		mysql_select_db($database_microassur, $microassur);
		$query_rs_lab_personne = "SELECT * FROM personne WHERE Identifiant_personne = '". $row_rs1['Identifiant_personne'] ."'";
		$rs_lab_personne = mysql_query($query_rs_lab_personne, $microassur) or die(mysql_error());
		$row_rs_lab_personne = mysql_fetch_assoc($rs_lab_personne);
		$totalRows_rs_lab_personne = mysql_num_rows($rs_lab_personne);
  ?>
    <tr <?php if($i%2==0) echo 'class="colorligne"'; ?>>
      <td class="col1">
      	<input onClick="modifier(this.value);" type="radio" name="choix" id="choix" value="<?php echo $row_rs1['Identifiant_personne']; ?>" />
      </td>	
      <td class="col2"><a href="#"><?php echo $row_rs1['Type_role']; ?></a></td>
	  <?php //$tab_typeAssure = array('AS'=>'Ascendant', 'AU'=>'Autre', 'C'=>'Conjoint', 'E'=>'Enfant', 'S'=>'Souscripteur'); ?>
      <td class="col3"><a href="#"><?php echo $row_rs1['Type_assure']; ?></a></td>
      <td class="col4"><a href="#"><?php echo $row_rs_lab_personne['Nom_personne']; ?></a></td>
      <td class="col5"><a href="#"><?php echo $row_rs_lab_personne['Prenom_personne']; ?></a></td>
      <td class="col6"><a href="#"><?php echo dateEN2FR($row_rs_lab_personne['Date_naissance_personne']); ?></a></td>
      <!--td class="col7">
      	<?php if(strcmp($motifEvenement, 'AL')==0 || strcmp($motifEvenement, 'PA')==0){ ?>
      	<a href="detailIntervenant.php?Identifiant_personne=<?php echo $row_rs1['Identifiant_personne']; ?>&Numero_contrat=<?php echo $numero_contrat; ?>&modif=<?php echo $modif; ?>">Modifier (A)</a>&nbsp;
        <?php }else{ ?>
        <a href="#">Modifier (I)</a>&nbsp;
        <?php } ?>
      	<a href="#">Visualiser</a>
      </td-->
    </tr>
    <?php $i++;
	} while ($row_rs1 = mysql_fetch_assoc($rs1)); 
mysql_free_result($rs1);
mysql_free_result($rs_lab_personne);
} // Fin if(isset($row_rs1['Identifiant_personne']) && !empty($row_rs1['Identifiant_personne']))
	?>
</table>

</div>

<div class="boutons-droite" style="float:right;">
	<div class="bt_right">
		<a id="modifAct" href="#">
		<span class="btleft"></span>
			<span class="btmiddle" style="padding-left:4px; padding-right:4px;">Modifier</span>
		<span class="btright"></span>
		</a>
	</div>  
	<div class="bt_right" style="clear:left;">
		<a id="suppAct" href="#">
		<span class="btleft"></span>
			<span class="btmiddle">Visualiser</span>
		<span class="btright"></span>
		</a>
	</div>  
</div>

<?php if(isset($afficherMotif) && $afficherMotif){ 
$tab_motif = array('AL'=>'Age limite atteint', 'CC'=>'Changement de couverture', 'CO'=>'Contractuelle', 'NP'=>'Non paiement', 'PA'=>'Plus d’assuré au contrat', 'RE'=>'Renonciation', 'S'=>'Sinistre');
?>
<table class="motifResil">
  <tr><td>&nbsp;</td></tr>
  <tr>
  	<td align="center">
    	<a href="#">MOTIF RESILIATION &nbsp;
        <input type="text" name="motifResiliation" value="<?php if(isset($motifEvenement)) echo $tab_motif[$motifEvenement]; ?>" disabled="disabled" /></a>
    </td>
  </tr>
</table>
<?php } ?>

	</div>
	<div class="imgright2"></div>
	<div id="br"></div>

<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">

  <div class="bt1">
	<a href="../../menu.php">
    	<span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a>
  </div>
  <div class="bt">
	<?php if(isset($_GET['modif']) && $_GET['modif']==1){ ?>
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $numero_contrat; ?>&modif=1&annul=0">
	<?php }else{ ?>
	<a href="../../menu.php">
	<?php } ?>
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
  </div>
  <div class="bt">
	<span class="btleft"></span>
	<input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
	<span class="btright"></span>
  </div>
  
  <input type="hidden" name="Identifiant_evenement" value="" />
  <input type="hidden" name="Type_evenement" value="G" /> <!-- G : Acte de gestion -->
  <input type="hidden" name="Sous_type_evenement" value="ARC" /> <!-- ARC : Réactivation contrat -->
  <input type="hidden" name="Motif_evenement" value="" />
  <input type="hidden" name="Reference_evenement" value="" />
  <input type="hidden" name="Date_evenement" value="<?php echo $date_du_jour ?>" />
  <input type="hidden" name="Identifiant_agent" value="<?php echo $_SESSION['iduser']; ?>" />
  <input type="hidden" name="Identifiant_sinistre" value="" />
  <input type="hidden" name="Identifiant_contrat" value="<?php echo $colname_rs1; ?>" />
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="Identifiant_echeance" value="" />
  <input type="hidden" name="Identifiant_prestation" value="" />
  <input type="hidden" name="MM_insert" value="form2" />
</form>
</div>
</body>
</html>
<?php


?>
